package ca.etsmtl.log730.projet2.connectors.clientserver.nodes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.UUID;

import ca.etsmtl.log730.projet2.documents.Document;
import ca.etsmtl.log730.projet2.users.User;

public class DocumentMap extends HashMap<UUID, Document> {

	@Override
	public Document put(UUID key, Document value) {
		if(!containsKey(key)) {
			editorQueue.put(key, new LinkedList<User>());
			viewers.put(key, new TreeSet<User>());
		}
		return super.put(key, value);
	}


	@Override
	public Document remove(Object key) {
		editorQueue.remove(key);
		viewers.remove(key);
		return super.remove(key);
	}


	/**
	 * 
	 */
	private static final long serialVersionUID = 305169637693389558L;

	
	private Map<UUID, LinkedList<User>> editorQueue = new TreeMap<UUID, LinkedList<User>>();
	private Map<UUID, TreeSet<User>> viewers = new TreeMap<UUID, TreeSet<User>>();
	
	
	public Set<User> getViewers(UUID id) {
		return viewers.get(id);
	}
	
	public LinkedList<User> getEditorQueue(UUID id) {
		return editorQueue.get(id);
	}
	
	
	private List<DocumentListener> documentListeners = new ArrayList<DocumentListener>();
	public List<DocumentListener> getDocumentListeners() {
		return documentListeners;
	}
	public void addDocumentListener(DocumentListener dl) {
		documentListeners.add(dl);
	}
	public void removeDocumentListener(DocumentListener dl) {
		documentListeners.remove(dl);
	}
	public static interface DocumentListener {
		public void updated(UUID id, Document d);
	}
}
